unit UnitNuevoInforme;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  sqldb, UnitMainForm, LR_Class, LR_DSet;

type

  { TFormNuevoInforme }

  TFormNuevoInforme = class(TForm)
    Button1: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    frReport1: TfrReport;
    frUserDataset1: TfrUserDataset;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Memo1: TMemo;
    SQLQuery1: TSQLQuery;
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
    procedure frUserDataset1CheckEOF(Sender: TObject; var Eof: Boolean);
    procedure frUserDataset1First(Sender: TObject);
    procedure frUserDataset1Next(Sender: TObject);
  private
    filas : longint;
  public
    pte_cdg : string;
    pte_dni : string;
    medsol_cdg : string;
    medsol_matricula : string;
    medej_cdg : string;
    medej_matricula : string;
    obsoc_cdg: string;

  end; 

var
  FormNuevoInforme: TFormNuevoInforme;

implementation

uses
  UnitBusquedaPaciente, UnitNuevoPaciente, UnitBusquedaMedico, UnitNuevoMedico, UnitBusquedaObraSocial,
    UnitBusquedaPreinforme;

{$R *.lfm}

{ TFormNuevoInforme }

procedure TFormNuevoInforme.FormShow(Sender: TObject);
begin

end;

procedure TFormNuevoInforme.frReport1GetValue(const ParName: String;
  var ParValue: Variant);
begin
  if ParName = 'fecha' then
    ParValue := Edit5.Text;

  if ParName = 'informe_nro' then
    ParValue := Label8.Caption;

  if ParName = 'paciente_nombre' then
    ParValue :=  Edit1.Text;

  if ParName = 'paciente_dni' then
    ParValue := pte_dni;

  if ParName = 'obra_social' then
    ParValue := Edit6.Text;

  if ParName = 'medico_solicitante_nombre' then
    ParValue := Edit2.Text;

  if ParName = 'medico_solicitante_matricula' then
    ParValue := medsol_matricula;

  if ParName = 'medico_ejecutor_nombre' then
    ParValue := Edit3.Text;

  if ParName = 'medico_ejecutor_matricula' then
    ParValue := medej_matricula;

  if ParName = 'titulo' then
    ParValue := Edit4.Text;

  if ParName = 'texto' then
    ParValue := Memo1.Lines[filas];
end;

procedure TFormNuevoInforme.frUserDataset1CheckEOF(Sender: TObject;
  var Eof: Boolean);
begin
  Eof := filas > Memo1.Lines.Count - 1
end;

procedure TFormNuevoInforme.frUserDataset1First(Sender: TObject);
begin
  filas := 0;
end;

procedure TFormNuevoInforme.frUserDataset1Next(Sender: TObject);
begin
  Inc(filas);
end;

procedure TFormNuevoInforme.Button1Click(Sender: TObject);
begin
  FormBusquedaPaciente.ShowModal;
end;

procedure TFormNuevoInforme.Button11Click(Sender: TObject);
begin
  FormBusquedaObraSocial.ShowModal;

  obsoc_cdg := FormBusquedaObraSocial.obsoc_codigo;
  Edit6.Text := FormBusquedaObraSocial.obsoc_nombre;
end;

procedure TFormNuevoInforme.Button10Click(Sender: TObject);
begin
  Close();
end;

procedure TFormNuevoInforme.Button12Click(Sender: TObject);
var
  nueva_obs : string;

begin
  nueva_obs := InputBox('Nueva Obra Social', 'Ingrese el nombre de la nueva Obra Social', '');

  if nueva_obs <> '' then
  begin
    nueva_obs := uppercase(nueva_obs);

    if SQLQuery1.Active = true then
      SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'insert into obra_social(obs_nombre) values(''' + nueva_obs + ''')';

    SQLQuery1.ExecSQL;

    SQLQuery1.SQL.Text := 'select * from obra_social';

    SQLQuery1.Open;

    SQLQuery1.ApplyUpdates;
    MainForm.SQLTransaction1.Commit;
    MainForm.ordenar_grid();

    SQLQuery1.Close;

    ShowMessage('Alta Exitosa');

    Edit6.Text := nueva_obs;

    SQLQuery1.SQL.Text := 'select obs_cdg from obra_social where obs_nombre = ''' + nueva_obs + '''';

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      obsoc_cdg := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;
  end;
end;

procedure TFormNuevoInforme.Button2Click(Sender: TObject);
begin
  FormNuevoPaciente.ShowModal;

  Edit1.Text := FormNuevoPaciente.Edit1.Text;
  pte_cdg := FormNuevoPaciente.nuevopte_codigo;
  pte_dni := FormNuevoPaciente.nuevopte_dni;

  if SQLQuery1.Active = true then
   SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from paciente';
  SQLQuery1.Open;

  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  SQLQuery1.Close;
end;

procedure TFormNuevoInforme.Button3Click(Sender: TObject);
begin
  FormBusquedaMedico.ShowModal;

  medsol_cdg := FormBusquedaMedico.medico_codigo;
  medsol_matricula := FormBusquedaMedico.medico_matricula;
  Edit2.Text := FormBusquedaMedico.medico_nombre;
end;

procedure TFormNuevoInforme.Button4Click(Sender: TObject);
begin
  FormNuevoMedico.ShowModal;

  Edit2.Text := FormNuevoMedico.Edit1.Text;
  medsol_cdg := FormNuevoMedico.nuevomedico_codigo;
  medsol_matricula := FormNuevoMedico.nuevomedico_matricula;

  if SQLQuery1.Active = true then
   SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from medico';
  SQLQuery1.Open;

  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  SQLQuery1.Close;
end;

procedure TFormNuevoInforme.Button5Click(Sender: TObject);
begin
  FormBusquedaMedico.ShowModal;

  medej_cdg := FormBusquedaMedico.medico_codigo;
  medej_matricula := FormBusquedaMedico.medico_matricula;
  Edit3.Text := FormBusquedaMedico.medico_nombre;
end;

procedure TFormNuevoInforme.Button6Click(Sender: TObject);
begin
  FormNuevoMedico.ShowModal;

  Edit3.Text := FormNuevoMedico.Edit1.Text;
  medej_cdg := FormNuevoMedico.nuevomedico_codigo;
  medej_matricula := FormNuevoMedico.nuevomedico_matricula;

  if SQLQuery1.Active = true then
   SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from medico';
  SQLQuery1.Open;

  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  SQLQuery1.Close;
end;

procedure TFormNuevoInforme.Button7Click(Sender: TObject);
begin
  FormBusquedaPreinforme.ShowModal;
end;

procedure TFormNuevoInforme.Button8Click(Sender: TObject);
var
  viejo : string;
begin
  if Label8.Caption <> '' then
    viejo := 'si'
  else
    viejo := 'no';

  if viejo = 'no' then
    SQLQuery1.SQL.Text := 'insert into informe(est_fecha, est_paciente, est_obra_social, est_medico_solicitante, '
      + 'est_medico_ejecutor, est_titulo, est_texto) values('''
      + FormatDateTime('yyyy-mm-dd', StrToDate(Edit5.Text)) + ''', ' + pte_cdg + ', ' + obsoc_cdg
      + ', ' + medsol_cdg + ', ' + medej_cdg + ', ''' + Edit4.Text + ''', ''' + Memo1.Text + ''')'
   else
    SQLQuery1.SQL.Text := 'update informe set est_fecha = ''' + FormatDateTime('yyyy-mm-dd', StrToDate(Edit5.Text))
      + ''', est_paciente = ' + pte_cdg + ', est_obra_social = ' + obsoc_cdg + ', est_medico_solicitante = '
      + medsol_cdg + ', est_medico_ejecutor = ' + medej_cdg + ', est_titulo = ''' + Edit4.Text
      + ''', est_texto = ''' + Memo1.Text + ''' where est_cdg = ' + Label8.Caption;

  SQLQuery1.ExecSQL;

  SQLQuery1.SQL.Text := 'select * from informe';
  SQLQuery1.Open;

  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  SQLQuery1.Close;

  if viejo = 'no' then
  begin
    SQLQuery1.SQL.Text := 'select max(est_cdg) from informe where est_paciente = ' + pte_cdg
      + ' and est_medico_solicitante = ' + medsol_cdg + ' and est_medico_ejecutor = ' + medej_cdg;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      Label8.Caption := SQLQuery1.Fields[0].AsString;
      Label8.Visible := true;
      SQLQuery1.Next
    end;

    SQLQuery1.Close;
  end;
end;

procedure TFormNuevoInforme.Button9Click(Sender: TObject);
begin
  frReport1.LoadFromFile('informe.lrf');
  frReport1.ShowReport;
end;

procedure TFormNuevoInforme.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

end.

